package com.mjh.zhujianju.controller.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.model.user.UserPass;
import com.mjh.zhujianju.util.ShiroEncryption;

@Controller(value = "com.mjh.zhujianju.controller.user.UserController")
@RequestMapping("user")
public class UserController extends BaseController<SysUser>{

	@Resource
	private SysUserService sysUserService;
	
	@Override
	protected BaseService<SysUser> getService() {
		return sysUserService;
	}
	
	

	/**
	 * 获取详情
	 */
	public AjaxResult findById(@PathVariable("id") Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		SysUser t = null;
		try {
			t = this.sysUserService.getByPrimaryKey(id);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(t);
	}
	
	
	/**
	 * 获取详情
	 */
	@RequestMapping(value="checkPhone",method=RequestMethod.POST)
	@ResponseBody
	public Boolean checkPhone(SysUser user) {
		if (user == null) {
			return Boolean.TRUE;
		}
		List<SysUser> t  = this.sysUserService.getChcekInfoByCondition(user);
		if(XTListUtils.isEmpty(t)) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value="modifyPass",method=RequestMethod.POST)
	@ResponseBody
	public AjaxResult modifyPass(@RequestBody UserPass userPass) {
		if(userPass == null){
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}

		if(StringUtils.isEmpty(userPass.getOldPassword())){
			return AjaxResult.getResult(Boolean.FALSE, "旧密码不能为空");
		}

		if(StringUtils.isEmpty(userPass.getNewPassword())){
			return AjaxResult.getResult(Boolean.FALSE, "新密码不能为空");
		}

		if(StringUtils.isEmpty(userPass.getRePassword())){
			return AjaxResult.getResult(Boolean.FALSE, "新密码不能为空");
		}

		if(!userPass.getNewPassword().equals(userPass.getRePassword())){
			return AjaxResult.getResult(Boolean.FALSE, "两次密码输入不一致，请重新输入");
		}

		//获取当前用户
		LoginUserInfo loginUserInfo = XTCurrentUser.getCurrentUser();
		if(loginUserInfo == null){
			return AjaxResult.getResult(Boolean.FALSE, "会话已失效，请重新登录");
		}

		SysUser sysUser = new SysUser();
		sysUser.setId(loginUserInfo.getId());
		sysUser.setPassword(ShiroEncryption.encrptText(userPass.getOldPassword()));
		sysUser.setRemark3(ShiroEncryption.encrptText(userPass.getNewPassword()));
		try {
			sysUserService.updatePwd(sysUser);
			return AjaxResult.getResult(Boolean.TRUE, "修改成功");
		} catch (XTBusinessException e) {
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
	}


}
